package com.privatework.backedserver.controller.chooseclass;

import com.privatework.common.entity.chooseclass.ChooseClassInfo;
import com.privatework.common.entity.classinfo.ClassInfo;
import com.privatework.common.repository.chooseclass.ChooseClassInfoRepository;
import com.privatework.common.repository.classinfo.ClassInfoRepository;
import com.privatework.common.requestvo.classroom.ChooseInfoReq;
import com.privatework.common.service.system.UserService;
import com.privatework.common.utils.ResultVoUtil;
import com.privatework.common.vo.ResultVo;
import com.privatework.common.vo.chooseclass.ChooseClassResultInfo;
import com.privatework.common.vo.system.UserResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * @author johnny
 * @create 2021-04-12 2:02 下午
 **/

@RestController
@Slf4j
@RequestMapping("/chooseClass")
public class ChooseClassInfoController {


    @Autowired
    private ChooseClassInfoRepository chooseClassInfoRepository;


    @Autowired
    private ClassInfoRepository classInfoRepository;


    @GetMapping("/byUserId")
    public ResultVo byUserId(@RequestParam("userId") Long userId, @RequestParam("relName") String relName) {
        List<ChooseClassInfo> chooseClassInfos = chooseClassInfoRepository.findByUserId(userId);

        List<ChooseClassResultInfo> chooseClassResultInfos = new ArrayList<>();
        chooseClassInfos.forEach(chooseClassInfo -> {

            ChooseClassResultInfo chooseClassResultInfo = new ChooseClassResultInfo();
            chooseClassResultInfo.setClassInfoId(chooseClassInfo.getClassInfoId());
            chooseClassResultInfo.setUserId(userId);
            chooseClassResultInfo.setUserRelName(relName);
            chooseClassResultInfo.setId(chooseClassInfo.getId());
            classInfoRepository.findById(chooseClassInfo.getClassInfoId()).ifPresent(classInfo -> {
                chooseClassResultInfo.setClassName(classInfo.getClassName());
                chooseClassResultInfo.setTeacherName(classInfo.getTeacherName());
                chooseClassResultInfo.setTotalCount(classInfo.getTotalCount());
            });
            chooseClassResultInfos.add(chooseClassResultInfo);
        });
        return ResultVoUtil.success(chooseClassResultInfos);
    }

    @GetMapping("/findAllClassInfo")
    public ResultVo findAllClassInfo() {

        List<ClassInfo> all = classInfoRepository.findAll();
        return ResultVoUtil.success(all);
    }


    @PostMapping("/submitChooseClassInfo")
    public ResultVo submitChooseClassInfo(@RequestBody ChooseInfoReq chooseInfoReq) {

        System.out.println(chooseInfoReq);

        if (chooseInfoReq.getUserId() != null && CollectionUtils.isNotEmpty(chooseInfoReq.getClassInfoIds())) {
            List<ClassInfo> classInfos = classInfoRepository.findAllById(chooseInfoReq.getClassInfoIds());
            classInfos.forEach(classInfo -> {
                ChooseClassInfo chooseClassInfo = new ChooseClassInfo();
                chooseClassInfo.setUserId(chooseInfoReq.getUserId());
                chooseClassInfo.setClassInfoId(classInfo.getId());
                chooseClassInfoRepository.save(chooseClassInfo);
            });
        }
        return ResultVoUtil.success();
    }

    @GetMapping("/delete")
    public ResultVo delete(@PathParam("id") Long id) {

        chooseClassInfoRepository.deleteById(id);

        return ResultVoUtil.success();
    }

}